#!/bin/sh

#脚本执行是在CentOS 6系统上调测的
#同级目录必须有openresty-1.11.2.2.tar.gz，sqlite-autoconf-3170000.tar.gz，lua-5.1.5.tar.gz，lsqlite3_fsl09x.zip，luafilesystem-v_1_6_3.tar.gz，lua-resty-session-2.15.tar.gz，lua-resty-template-1.9.tar.gz，LuaBitOp-1.0.2.tar.gz几个文件

#安装依赖库
yum install pcre pcre-devel
yum install readline readline-devel
yum install openssl openssl-devel
yum install unzip

#指定安装目录，openresty默认就是安装在/usr/local/openresty
OPENRESTY_ROOT=/usr/local/openresty

#安装openresty
tar -zxvf openresty-1.11.2.2.tar.gz
cd openresty-1.11.2.2
./configure --prefix=$OPENRESTY_ROOT || exit 1
make || exit 1
make install || exit 1
cd ..
rm -rf openresty-1.11.2.2

#创建扩展库和头文件的存放位置
mkdir $OPENRESTY_ROOT/lib
mkdir $OPENRESTY_ROOT/include

#拷贝lua头文件，luagit暂时使用lua-5.1.5的头文件
tar -zxvf lua-5.1.5.tar.gz
cd ./lua-5.1.5
cp -rf ./src/lua.h $OPENRESTY_ROOT/include
cp -rf ./src/luaconf.h $OPENRESTY_ROOT/include
cp -rf ./src/lualib.h $OPENRESTY_ROOT/include
cp -rf ./src/lauxlib.h $OPENRESTY_ROOT/include
cd ..
rm -rf ./lua-5.1.5

#编译sqlite3，并拷贝头文件和库
tar -zxvf sqlite-autoconf-3170000.tar.gz
cd sqlite-autoconf-3170000
./configure || exit 1
make || exit 1
cp -rf ./sqlite3ext.h $OPENRESTY_ROOT/include
cp -rf ./sqlite3.h $OPENRESTY_ROOT/include
cp -rf ./.libs/libsqlite3* $OPENRESTY_ROOT/lib
cd ..
rm -rf sqlite-autoconf-3170000

#编译lsqlite3，并拷贝lsqlite3.so到openresty的lualib目录
unzip lsqlite3_fsl09x.zip
cd lsqlite3_fsl09x
#-rpath把运行时搜索库的路径写进动态库里面去，-I指定编译时搜索头文件路径，-L指定链接时搜索库的路径
gcc -Wl,-rpath,'../../lib:../../luajit/lib' -fPIC -shared ./lsqlite3.c -o ./lsqlite3.so -I$OPENRESTY_ROOT/include -L$OPENRESTY_ROOT/lib -L$OPENRESTY_ROOT/luajit/lib -lsqlite3 -lluajit-5.1
cp -rf ./lsqlite3.so $OPENRESTY_ROOT/lualib/
cd ..
rm -rf lsqlite3_fsl09x

#集成luafilesystem库
tar -zxvf luafilesystem-v_1_6_3.tar.gz
cd luafilesystem-v_1_6_3/src
gcc -Wl,-rpath,'../../lib:../../luajit/lib' -fPIC -shared ./lfs.c -o ./lfs.so -I$OPENRESTY_ROOT/include  -L$OPENRESTY_ROOT/luajit/lib -lluajit-5.1
cp -rf ./lfs.so $OPENRESTY_ROOT/lualib/
cd ../..
rm -rf luafilesystem-v_1_6_3

#集成session库
tar -zxvf lua-resty-session-2.15.tar.gz
cd lua-resty-session-2.15/lib
cp -rf ./* $OPENRESTY_ROOT/lualib/
cd ../..
rm -rf lua-resty-session-2.15

#集成模板库
tar -zxvf lua-resty-template-1.9.tar.gz
cd lua-resty-template-1.9/lib
cp -rf ./* $OPENRESTY_ROOT/lualib/
cd ../..
rm -rf lua-resty-template-1.9

#集成LuaBitOp库
tar -zxvf LuaBitOp-1.0.2.tar.gz
cd LuaBitOp-1.0.2
gcc -Wl,-rpath,'../../lib:../../luajit/lib' -fPIC -shared ./bit.c -o ./bit.so -I$OPENRESTY_ROOT/include -L$OPENRESTY_ROOT/luajit/lib -lluajit-5.1
cp -rf ./bit.so $OPENRESTY_ROOT/lualib/
cd ..
rm -rf LuaBitOp-1.0.2

#添加lua脚本映射配置
cat > $OPENRESTY_ROOT/nginx/conf/handler.conf <<EOF
		location ~ ^/([-_a-zA-Z0-9/]+)\.lp\$ {
			default_type 'text/html';
			set \$path \$1;
			content_by_lua_file html/\$path.lua;
		}
		#屏蔽lua文件的下载，防止后端代码被看到
		location ~ ^/([-_a-zA-Z0-9/]+)\.lua\$ {
			default_type 'text/html';
			set \$path \$1;
			content_by_lua_file html/\$path.lua;
		}
EOF

grep "include ./handler.conf;" $OPENRESTY_ROOT/nginx/conf/nginx.conf > /dev/null
if [ 0 -ne $? ]; then
	sed -i "/# proxy the PHP scripts to Apache listening on/iinclude ./handler.conf;\n" $OPENRESTY_ROOT/nginx/conf/nginx.conf
fi

echo "install path:$OPENRESTY_ROOT!"
